<template>
  <div>
    <SearchForm
        @search="onSearch"
        @reset="onReset"
        :search-list="searchList" :search-params="searchParams">
    </SearchForm>

    <div class="table-layout">
      <el-card shadow="never">
        <div class="table-handle">
          <el-button type="primary" size="mini" icon="el-icon-plus" @click="onAdd">新增</el-button>
        </div>
        <div class="table-box">
          <el-table
              size="large"
              stripe
              border
              v-loading="loading"
              max-height="500px"
              :data="tableData"
              row-key="id"
              element-loading-spinner="el-icon-loading"
              element-loading-background="rgba(255, 255, 255, 0.8)"
              @selection-change="onSelect"
          >
            
            <el-table-column prop="cover" label="封面" width="120">
              <template slot-scope="scope">
                <el-image
                    :preview-src-list="[scope.row.cover]"
                    style="width: 80px; height: 80px"
                    :src="scope.row.cover" fit="contain">
                  <template #error>
                    <img src="@/assets/file.png" alt="" style="width: 80px; height: 80px">
                  </template>
                </el-image>
              </template>
            </el-table-column>
            <el-table-column prop="name" label="商品名称"/>
            <el-table-column prop="category.name" label="分类" width="120"/>
            <el-table-column prop="sort" label="排序" width="80"/>
            <el-table-column prop="status" label="状态" width="100">
              <template slot-scope="scope">
                <el-tag v-if="scope.row.status === 1" type="success">上架</el-tag>
                <el-tag v-else type="info">下架</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="is_virtual" label="是否置顶" width="100">
              <template slot-scope="scope">
                <el-tag v-if="scope.row.is_top === 1" type="warning">是</el-tag>
                <el-tag v-else type="primary">否</el-tag>
              </template>
            </el-table-column>
            <el-table-column label="操作" fixed="right" min-width="250">
              <template slot-scope="scope">
                <el-button type="text" @click="onEdit(scope.row)">编辑</el-button>
                <el-button type="text" @click="onSku(scope.row)">SKU管理</el-button>
                <!-- <el-button type="text" @click="onSkuBatch(scope.row)">批量SKU</el-button> -->
                <el-button type="text" style="color: #F82222" @click="onDelete(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-card>
      <div class="page-box">
        <el-pagination
            background
            :current-page="pageNo"
            :page-sizes="pageSizes"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalSize"
            @size-change="changePageSize"
            @current-change="changePageNo"
        />
      </div>
    </div>
    <Add ref="addFormRef" @get-page="initData" />
    <SkuManager ref="skuManagerRef" />
    <SkuBatchManager ref="skuBatchManagerRef" />
  </div>
</template>
<script>

import crud from "@/mixins/crud";
import { http } from "@/api";
import Add from "./add";
import SkuManager from "./sku";
import SkuBatchManager from "./skuBatch";

export default {
  name: "ProductIndex",
  components: {
    Add,
    SkuManager,
    SkuBatchManager
  },
  mixins: [crud],
  data() {
    return {
      request: http.product,
      searchList: [
        {
          label: '商品名称',
          prop: 'name',
          name: 'input',
          placeholder: '请输入商品名称'
        },
        {
          label: '商品分类',
          prop: 'category_id',
          name: 'select',
          placeholder: '请选择分类',
          options: []
        },
        {
          label: '状态',
          prop: 'status',
          name: 'select',
          placeholder: '请选择状态',
          options: [
            { value: 1, label: '上架' },
            { value: 0, label: '下架' }
          ]
        }
      ],
      searchParams: {
        name: '',
        category_id: '',
        status: ''
      },
      defaultParams: {
      },
      tableData: []
    }
  },
  mounted() {
    this.loadCategories();
  },
  methods: {
    loadCategories() {
      // 加载商品分类
      http.category.list().then(res => {
        this.searchList[1].options = res.data.map(item => ({
          value: item.id,
          label: item.name
        }));
      });
    },
    onSku(row) {
      this.$refs.skuManagerRef.open(row);
    },
    onSkuBatch(row) {
      this.$refs.skuBatchManagerRef.open(row);
    }
  }
}
</script>

<style scoped lang="scss">
.table-layout {
  margin-top: 20px;
}
.table-handle {
  margin-bottom: 20px;
}
.page-box {
  margin-top: 20px;
  text-align: right;
}
</style>
